package com.chuangshu.chuangshuteam.controller;

import com.chuangshu.chuangshuteam.annotation.RequiresPermissions;
import com.chuangshu.chuangshuteam.config.BaseContext;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.dto.TeamInfoDTO;
import com.chuangshu.chuangshuteam.dto.TeamMemberListDTO;
import com.chuangshu.chuangshuteam.dto.TeamUpdateRequest;
import com.chuangshu.chuangshuteam.service.AuthService;
import com.chuangshu.chuangshuteam.service.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private AuthService authService;

    // 默认团队ID（单团队模式）
    private static final Long DEFAULT_TEAM_ID = 1L;

    /**
     * 查看团队基本信息
     */
    @GetMapping("/info")
    public Result getTeamInfo() {
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            return Result.fail("用户未登录");
        }

        // 检查是否已加入团队
        if (!authService.isUserInTeam(currentUserId)) {
            return Result.fail("未加入团队");
        }

        TeamInfoDTO teamInfo = teamService.getTeamInfo(DEFAULT_TEAM_ID);
        return Result.ok(teamInfo);
    }

    /**
     * 编辑团队信息（仅管理员）
     */
    @PutMapping("/info")
    @RequiresPermissions("team:edit")
    public Result updateTeamInfo(@RequestBody TeamUpdateRequest request) {
        Long currentUserId = BaseContext.getCurrentId();
        if (!authService.isAdmin(currentUserId)) {
            return Result.fail("无权限操作");
        }

        boolean success = teamService.updateTeamInfo(DEFAULT_TEAM_ID, request);
        return success ? Result.ok("团队信息更新成功") : Result.fail("更新失败");
    }

    /**
     * 查看团队成员列表及方向分布
     */
    @GetMapping("/members")
    public Result getTeamMembers(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size,
            @RequestParam(required = false) String direction) {
        
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            return Result.fail("用户未登录");
        }

        if (!authService.isUserInTeam(currentUserId)) {
            return Result.fail("未加入团队");
        }

        TeamMemberListDTO result = teamService.getTeamMembers(
                DEFAULT_TEAM_ID,
                page,
                size,
                direction,
                currentUserId
        );
        return Result.ok(result);
    }

    /**
     * 重置邀请码（仅管理员）
     */
    @PostMapping("/invite/reset")
    @RequiresPermissions("team:invite:reset")
    public Result resetInviteCode() {
        Long currentUserId = BaseContext.getCurrentId();
        if (!authService.isAdmin(currentUserId)) {
            return Result.fail("无权限操作");
        }

        String newInviteCode = teamService.resetInviteCode(DEFAULT_TEAM_ID);
        return newInviteCode != null ? 
                Result.ok(newInviteCode) : 
                Result.fail("重置邀请码失败");
    }
}